package x.ovo.wechat.bot.http.request.message;

import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.json.JSONUtil;
import x.ovo.wechat.bot.common.Constant;
import x.ovo.wechat.bot.common.entity.message.SendMessage;
import x.ovo.wechat.bot.common.http.request.ApiRequest;
import x.ovo.wechat.bot.common.http.session.LoginSession;

import java.util.Map;
import java.util.function.Function;

public class SendImageRequest extends ApiRequest<Boolean> {

    private final SendMessage message;

    public SendImageRequest(LoginSession session, SendMessage message) {
        super(session);
        this.message = message;
    }

    @Override
    public String getMethod() {
        return POST;
    }

    @Override
    public boolean isJson() {
        return true;
    }

    @Override
    public String getUrl() {
        return UrlBuilder.of(this.session.getUrl())
                .addPath(Constant.SEND_IMAGE)
                .addQuery("fun", "async")
                .addQuery("f", "json")
                .addQuery("pass_ticket", this.session.getPassTicket())
                .build();
    }

    @Override
    public Map<String, Object> getParameters() {
        super.addParameter("BaseRequest", this.baseRequest)
                .addParameter("Scene", 0)
                .addParameter("Msg", this.message);
        return super.getParameters();
    }

    @Override
    public Function<String, Boolean> responseHandler() {
        return s -> JSONUtil.parseObj(s).getJSONObject("BaseResponse").getInt("Ret") == 0;
    }
}
